﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using GPSTrackingWebsiteManager.ClassManager;
using Telerik.Web.UI;

namespace GPSTrackingWebsiteManager.Manager.UserControls
{
    public partial class RolesSearch : System.Web.UI.UserControl
    {
        //Events
        public delegate void AcceptEvent(List<GridDataItem> rows);
        public event AcceptEvent OnAcceptEvent;

        public delegate void CancelEvent();
        public event CancelEvent OnCancelEvent;

        //Propeties
        [Browsable(true), Category("Custom")]
        public bool IsMultipleSelects { get; set; }

        [Browsable(true), Category("Custom")]
        public string CalledBy { get; set; }
        
        [Browsable(true), Category("Custom")]
        public bool IsClearGridAfterClose { get; set; }

        //Events
        protected void Page_Load(object sender, EventArgs e)
        {
            RolesGrid.AllowMultiRowSelection = IsMultipleSelects;

            if (!IsPostBack)
            {
                GridSortExpression expression = new GridSortExpression();
                expression.FieldName = GPSTrackingDBV2.Entities.AdministratorsRolesColumn.Name.ToString();
                expression.SetSortOrder("Ascending");
                this.RolesGrid.MasterTableView.SortExpressions.AddSortExpression(expression);
                
                if (IsClearGridAfterClose)
                    clearRolesGrid();
            }

        }

        protected void btnSearchRoles_Click(object sender, EventArgs e)
        {
            getInfoGrid();
            RolesGrid.DataBind();
        }

        protected void btnRolesOK_Click(object sender, EventArgs e)
        {
            List<GridDataItem> rows = new List<GridDataItem>();
            foreach (Telerik.Web.UI.GridDataItem selectedItem in RolesGrid.SelectedItems)
            {
                rows.Add(selectedItem);
            }

            if (IsClearGridAfterClose)
                clearRolesGrid();

            OnAcceptEvent(rows);
        }

        protected void btnRolesCancel_Click(object sender, EventArgs e)
        {
            if (IsClearGridAfterClose)
                clearRolesGrid();
            OnCancelEvent();
        }

        //Methods

        public void clearRolesGrid()
        {
            RolesGrid.DataSource = new string[] { };
            RolesGrid.DataBind();
        }
        
        public void getRolesToSelectAdmin(){
            RolesGrid.DataSource = GPSTrackingDBV2Custom.Data.DataRepository.RolesProvider.GetAllToAssociateUsers_Find(getSessionManager().CompanyIDSelectedComboBox, getSessionManager().AdminUsernameForRole, txtName.Text, false, true, false);
        }

        public void getAdministratorsRolesToSelect()
        {
            RolesGrid.DataSource = GPSTrackingDBV2Custom.Data.DataRepository.AdministratorsRolesProvider.GetAllToAssociateCompanies_Find(getSessionManager().CompanyIDSelectedComboBox, txtName.Text);
        }

        public void getRolesToSelectUser()
        {
            RolesGrid.DataSource = GPSTrackingDBV2Custom.Data.DataRepository.RolesProvider.GetAllToAssociateUsers_Find(getSessionManager().CompanyIDSelectedComboBox, getSessionManager().UsernameForRole, txtName.Text, true, false, false);
        }

        public void getRolesToSelectMaster()
        {
            RolesGrid.DataSource = GPSTrackingDBV2Custom.Data.DataRepository.RolesProvider.GetAllToAssociateUsers_Find(getSessionManager().CompanyIDSelectedComboBox, getSessionManager().MasterUsernameForRole, txtName.Text, false, false, true);
        }

        protected SessionManager getSessionManager()
        {
            return (SessionManager)HttpContext.Current.Session["SessionManager"];
        }

        protected void getInfoGrid()
        {
            if (CalledBy == "AdministratorsRolesByAdministrator")
            {
                getRolesToSelectAdmin();
            }
            if (CalledBy == "UserRolesByUsers")
            {
                getRolesToSelectUser();
            }
            if (CalledBy == "MastersRolesByMaster")
            {
                getRolesToSelectMaster();
            }
            if (CalledBy == "CompaniesByAdministratorsRoles")
            {
                getAdministratorsRolesToSelect();
            }
        }

        protected void RolesGrid_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            getInfoGrid();
        }
        
    }
}